<?php
/**
 * @title 2707. 字符串中的额外字符
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $s
     * @param String[] $dictionary
     * @return Integer
     */
    function minExtraChar($s, $dictionary) {
        /**
         * @since 2024-01-09 哈希字典，长度数组
         */
        $this->hashArray = $this->lenArray = [];
        foreach ($dictionary as $word){
            $this->hashArray[$word] = true;
            $this->lenArray[strlen($word)] = strlen($word);
        }

        /**
         * @since 2024-01-09 长度升序排列
         */
        sort($this->lenArray);

        /**
         * @since 2024-01-09 防止反复遍历
         */
        $this->dpArray = [];

        /**
         * @since 2024-01-09 递归
         * @return Integer
         */
        return $this->MinMissCount($s, 0, strlen($s)-1);
    }

    /**
     * @title 返回[s,e]之间，最小的缺失字符数
     * @author start2004
     * @since 2024-01-09 Created
     *
     * @param String $str
     * @param Integer $s
     * @param Integer $e
     * @return Integer
     */
    function MinMissCount($str, $s, $e){
        /**
         * @since 2024-01-09 命中缓存
         */
        if(isset($this->dpArray[$s])) {
            return $this->dpArray[$s];
        } else {}

        /**
         * @since 2024-01-09 越界，返回0
         */
        if($s > $e){
            return 0;
        } else {}

        /**
         * @since 2024-01-09 最小缺失字符数
         */
        $minMissCount = PHP_INT_MAX;

        /**
         * @since 2024-01-09 遍历lenArray，切割字符串，查找是否存在
         */
        foreach ($this->lenArray as $len){
            /**
             * @since 2024-01-09 越界，lenArray升序，后面的len不用遍历
             */
            if($s+$len-1 > $e){
                break;
            } else {}

            /**
             * @since 2024-01-09 切[s, s+len-1]区间字符串
             */
            $subStr = substr($str, $s, $len);
            if(isset($this->hashArray[$subStr])){
                /**
                 * @since 2024-01-09 命中字典，递归
                 */
                $minMissCount = min($minMissCount, $this->MinMissCount($str, $s+$len, $e));
            } else {}
        }

        /**
         * @since 2024-01-09 跳过s字符
         */
        $minMissCount = min($minMissCount, 1+$this->MinMissCount($str, $s+1, $e));

        /**
         * @return
         */
        return $this->dpArray[$s] = $minMissCount;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.09.php
 */
$datas = [
    ["leetscode", ["leet","code","leetcode"]], // 1
    ["sayhelloworld", ["hello","world"]], // 3
    ["leetcodes", ["leetcode", "leet", "codes"]], // 0
    ["ccodess", ["ccode", "codess"]], // 1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minExtraChar(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
